/*
 * Copyright (c) 2017-present Ilhasoft.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package br.com.ilhasoft.support.validation.rule;


import ohos.agp.components.Component;

/**
 * 校验规则
 * Created by john-mac on 5/14/16.
 */
public abstract class Rule<ViewType extends Component, ValueType> {
    //规则类型
    protected ValueType value;
    //输入内容
    protected String content;
    //显示控件
    protected ViewType view;
    //显示控件
    protected ViewType view1;
    //错误信息
    protected String errorMessage;

    public Rule(ViewType view, String content,ValueType value, String errorMessage) {
        this.view = view;
        this.content = content;
        this.value = value;
        this.errorMessage = errorMessage;
    }

    public Rule(ViewType view, ViewType view1, ValueType value, String errorMessage) {
        this.view = view;
        this.value = value;
        this.view1 = view1;
        this.errorMessage = errorMessage;
    }

    /**
     * 进行校验
     * @return true&false
     */
    public final boolean validate() {
        final boolean valid = isValid(view);
        if (valid) {
            onValidationSucceeded(view);
        } else {
            onValidationFailed(view);
        }
        return valid;
    }

    /**
     * 校验
     * @param view view
     * @return true&false
     */
    protected abstract boolean isValid(ViewType view);

    protected void onValidationSucceeded(ViewType view) {}

    protected void onValidationFailed(ViewType view) {}

    public ViewType getView() {
        return view;
    }
}
